<?php
namespace app\system\controller;

use app\common\model\AuthGroupModel;
use app\common\model\SiteModel;
use app\common\model\UserModel;
use think\helper\Str;
use think\Request;

class AdminController extends BaseController
{
    private $userModel;
    private $authGroupModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->userModel      = new UserModel();
        $this->authGroupModel = new AuthGroupModel();
    }

    /**
     * 会员首页
     * @return mixed
     */
    public function index()
    {
        $map          = [];
        $query        = [];
        $searchKey    = $this->request->param('searchKey');
        $searchText   = $this->request->param('searchText');
        if (!empty($searchKey) && !empty($searchText)) {
            $map[$searchKey]     = ['like', '%' . $searchText . '%'];
            $query['searchKey']  = $searchKey;
            $query['searchText'] = $searchText;
        }
        $list = $this->userModel->where('site_id','-1')->where($map)->order('uid desc')->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加用户
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $data['site_id'] = $this->getSiteId();
            $data['utype'] = $this->getUtype();
            $result = $this->validate($data, 'AdminValidate.add');
            if (true !== $result) {
                $this->error($result);
            }
            $result = $this->userModel->addData($data);
            if($result===false){
                $this->error($this->userModel->getError());
            }else{
                $this->success('添加成功！',url('index'));
            }
        }
        $groupList = $this->authGroupModel->where('utype', $this->getUtype())->order('group_sort asc')->select();
        $this->assign('groupList', $groupList);
        return $this->fetch();
    }

    /**
     * 修改数据
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $data['site_id'] = $this->getSiteId();
            $data['utype'] = $this->getUtype();
            $result = $this->validate($data, 'AdminValidate.edit');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->userModel->editData($data)) {
                $this->success('修改成功！', url('index'));
            } else {
                $this->error($this->userModel->getError());
            }
        }
        $uid       = $this->request->param('uid');
        $info      = $this->userModel->get($uid);
        $groupList = $this->authGroupModel->where('utype', $this->getUtype())->order('group_sort asc')->select();
        $this->assign('groupList', $groupList);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除数据
     */
    public function setStatus()
    {
        $uid = $this->request->param('uid', 0);
        if ($this->userModel->setStatus($uid)) {
            $this->success('操作成功！');
        } else {
            $this->error($this->userModel->getError());
        }
    }

}
